﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Query;
using HZ.Supervise.Model.Enum;

public partial class Company_NoSendOnTimeCompanyPS : BasePage
{
    public Company_NoSendOnTimeCompanyPS()
    {
        this.ModuleNo = Module.履约管理.ToString();
        this.PageOperateType = OperateType.未按时配送的企业.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //判断是否当前的年月
            //2011.4.13 张雄 modify
            string year = DateTime.Now.Year.ToString();
            string month = DateTime.Now.Month.ToString();
            //如果是的话就显示当前的年月
            foreach (ListItem item in this.ddlYear.Items)
            {
                if (item.Text == year.ToString())
                {
                    item.Selected = true;
                    break;
                }
            }
            foreach (ListItem item1 in this.ddlMonth.Items)
            {
                if (item1.Text == month.ToString())
                {
                    item1.Selected = true;
                    break;
                }
            }
            BindTime();
            //BindArea();
            this.AreaControl1.InitAreaID = CurrentAccount.GetUser().Org.Area.AreaID;//初始化地区
            BindData();
            GetNoSendOnTimeCount();

        }
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        //BindTime();
        GetNoSendOnTimeCount();
    }
    protected void BindTime()
    {
        ddlYear.SelectedItem.Text = DateTime.Now.Year.ToString();
        ddlMonth.SelectedItem.Text = DateTime.Now.Month.ToString();
    }


    protected void BindData()
    {
        try
        {
            int TotalRecord = 0;
            HZ.Supervise.IBLL.IBLL_ProcureCatalog ip = new HZ.Supervise.BLL.BLL_ProcureCatalog();

            HZ.Supervise.Model.Query.UnDistributeCompanyQuery ucq = new HZ.Supervise.Model.Query.UnDistributeCompanyQuery();

            ucq.CompanyName_PS = HZ.Fun.CheckString(txtCompanyName_PS.Text.Trim());
            // ucq.HospitalName = HZ.Fun.CheckString(txtHospitalName.Text.Trim());

            //ucq.AreaName = HZ.Fun.CheckString(txtArea.Text.Trim());

            if (!string.IsNullOrEmpty(AreaControl1.SelectedAreaID) && AreaControl1.SelectedAreaID != "510000")
            {
                ucq.AreaID = AreaControl1.SelectedAreaID;
            }
            //if (ddlCity.SelectedValue == "")
            //{
            //    ucq.AreaID = CurrentAccount.GetUser().Org.Area.AreaID == Constants.CURRENT_DEPLOY_AREAID ? "" : CurrentAccount.GetUser().Org.Area.AreaID;
            //}
            //else
            //{
            //    ucq.AreaID = ddlCity.SelectedValue;
            //}


            //string datestrFrom = ddlYear.SelectedValue.ToString() + "-" + ddlMonth.SelectedValue.ToString() + "-1";
            //string daterstrTo = ddlYear.SelectedValue.ToString() + "-" + (int.Parse(ddlMonth.SelectedValue.ToString()) + 1).ToString() + "-1";
            //SelectedItem获得值
            string datestrFrom = ddlYear.SelectedItem.Text + "-" + ddlMonth.SelectedItem.Text + "-1";
            string daterstrTo = ddlYear.SelectedItem.Text + "-" + (int.Parse(ddlMonth.SelectedItem.Text) + 1).ToString() + "-1";
            ucq.PlanTimeFrom = new HZ.Common.ConvertHelper(datestrFrom).ToDateTime;
            ucq.PlanTimeTo = new HZ.Common.ConvertHelper(daterstrTo).ToDateTime;

            ucq.SortFields = " UnDistributeAmount DESC";

            ViewState["UQuery"] = ucq;

            DataTable dt = ip.GetUnDistributeCompanyList(ucq, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out TotalRecord);

            AspNetPager1.RecordCount = TotalRecord;
            gvwInfo.DataSource = dt;
            gvwInfo.DataBind();

            setChart(dt);

        }
        catch (Exception e)
        {
            e.ToString();

            HZ.Web.WebUtil.Alert("查询条件请正确填写！");
        }
    }


    protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
    {
        BindData();
    }

    //protected void gvwInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "DirectByCom")
    //    {
    //        string sessionid = Guid.NewGuid().ToString();
    //        UnDistributeCompanyQuery query = (UnDistributeCompanyQuery)ViewState["UQuery"];

    //        //query.CompanyID_PS = e.CommandArgument.ToString();
    //        string[] company_PS = e.CommandArgument.ToString().Split(',');
    //        query.CompanyID_PS = company_PS[0].ToString();
    //        query.CompanyName_PS = company_PS[1].ToString();

    //        Session[sessionid] = query;

    //        string url = System.Configuration.ConfigurationManager.AppSettings["NoSendDetail"];
    //        Response.Redirect(string.Format("{0}?sessionnosend={1}&QueryPrompt={2}", url, sessionid, company_PS[1].ToString()));
    //    }
    //}

    /// <summary>
    /// 设置图表
    /// </summary>
    /// <param name="?"></param>
    private void setChart(System.Data.DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            this.ChartControl1.Visible = false;
            HZ.Supervise.Web.WebService.ChartConfig serChart = new HZ.Supervise.Web.WebService.ChartConfig();
            dt.TableName = "未配送";
            serChart.DataSource = dt;

            serChart.XName = "CompanyName_PS";
            serChart.YNames = "UnDistributeAmount";
            serChart.ChartTypes = HZ.Supervise.Web.WebService.ChartType.PieChart;

            ChartControl1.webChart = serChart;
            ChartControl1.PageId = Guid.NewGuid().ToString();
            ChartControl1.EntranceChart();
        }
        else
        {
            ChartControl1.Visible = false;
        }
    }
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        int TotalRecord = 0;
        HZ.Supervise.IBLL.IBLL_ProcureCatalog ip = new HZ.Supervise.BLL.BLL_ProcureCatalog();
        HZ.Supervise.Model.Query.UnDistributeCompanyQuery ucq = new HZ.Supervise.Model.Query.UnDistributeCompanyQuery();

        ucq.CompanyName_PS = HZ.Fun.CheckString(txtCompanyName_PS.Text.Trim());
        //ucq.HospitalName = HZ.Fun.CheckString(txtHospitalName.Text.Trim()); 

        if (!string.IsNullOrEmpty(AreaControl1.SelectedAreaID) && AreaControl1.SelectedAreaID != "510000")
        {
            ucq.AreaID = AreaControl1.SelectedAreaID;
        }
        //if (ddlCity.SelectedValue == "")
        //{
        //    //string areaidstr = string.Empty;
        //    //for (int i = 1; i < ddlCity.Items.Count; i++)
        //    //{
        //    //    areaidstr += "," + ddlCity.Items[i].Value;
        //    //}
        //    //areaidstr = areaidstr.Substring(1, areaidstr.Length - 1);
        //    //ucq.AreaID = "";
        //    //ucq.Append = " AreaID IN ('" + areaidstr.Replace("'", "").Replace(",", "','") + "') ";
        //    ucq.AreaID = CurrentAccount.GetUser().Org.Area.AreaID == Constants.CURRENT_DEPLOY_AREAID ? "" : CurrentAccount.GetUser().Org.Area.AreaID;
        //}
        //else
        //{
        //    ucq.AreaID = ddlCity.SelectedValue;
        //}


        string datestrFrom = ddlYear.SelectedValue.ToString() + "-" + ddlMonth.SelectedValue.ToString() + "-1";
        string daterstrTo = ddlYear.SelectedValue.ToString() + "-" + (int.Parse(ddlMonth.SelectedValue.ToString()) + 1).ToString() + "-1";
        ucq.PlanTimeFrom = new HZ.Common.ConvertHelper(datestrFrom).ToDateTime;
        ucq.PlanTimeTo = new HZ.Common.ConvertHelper(daterstrTo).ToDateTime;

        ucq.SortFields = "CompanyID_PS ASC";

        ViewState["UQuery"] = ucq;

        DataTable dt = ip.GetUnDistributeCompanyList(ucq, 0, 0, out TotalRecord);

        ExportFile.Instance.ExportData(dt, gvwInfo, this.Page);
    }
    protected void lbtnSee_Click(object sender, EventArgs e)
    {
        if (this.lbtnSee.Text == "显示图表")
        {
            ChartControl1.Visible = true;
            this.lbtnSee.Text = "隐藏图表";
        }
        else
        {
            ChartControl1.Visible = false;
            this.lbtnSee.Text = "显示图表";
        }
    }
    public void GetNoSendOnTimeCount()
    {
        int TotalRecord = 0;
        HZ.Supervise.IBLL.IBLL_ProcureCatalog ip = new HZ.Supervise.BLL.BLL_ProcureCatalog();

        HZ.Supervise.Model.Query.UnDistributeCompanyQuery ucq = new HZ.Supervise.Model.Query.UnDistributeCompanyQuery();

        ucq.CompanyName_PS = HZ.Fun.CheckString(txtCompanyName_PS.Text.Trim());
        // ucq.HospitalName = HZ.Fun.CheckString(txtHospitalName.Text.Trim());

        //ucq.AreaName = HZ.Fun.CheckString(txtArea.Text.Trim());
        if (!string.IsNullOrEmpty(AreaControl1.SelectedAreaID) && AreaControl1.SelectedAreaID != "510000")
        {
            ucq.AreaID = AreaControl1.SelectedAreaID;
        }
        //if (ddlCity.SelectedValue == "")
        //{
        //    //string areaidstr = string.Empty;
        //    //for (int i = 1; i < ddlCity.Items.Count; i++)
        //    //{
        //    //    areaidstr += "," + ddlCity.Items[i].Value;
        //    //}
        //    //areaidstr = areaidstr.Substring(1, areaidstr.Length - 1);
        //    ucq.AreaID = CurrentAccount.GetUser().Org.Area.AreaID == Constants.CURRENT_DEPLOY_AREAID ? "" : CurrentAccount.GetUser().Org.Area.AreaID;
        //    //ucq.Append = " AreaID IN ('" + areaidstr.Replace("'", "").Replace(",", "','") + "') ";
        //}
        //else
        //{
        //    ucq.AreaID = ddlCity.SelectedValue;
        //}


        string datestrFrom = ddlYear.SelectedValue.ToString() + "-" + ddlMonth.SelectedValue.ToString() + "-1";
        string daterstrTo = ddlYear.SelectedValue.ToString() + "-" + (int.Parse(ddlMonth.SelectedValue.ToString()) + 1).ToString() + "-1";
        ucq.PlanTimeFrom = new HZ.Common.ConvertHelper(datestrFrom).ToDateTime;
        ucq.PlanTimeTo = new HZ.Common.ConvertHelper(daterstrTo).ToDateTime;

        ucq.SortFields = "CompanyID_PS ASC";

        ViewState["UQuery"] = ucq;

        DataTable dt = ip.GetUnDistributeCompanyList(ucq, AspNetPager1.CurrentPageIndex, 0, out TotalRecord);
        int PurchaseCount = 0;
        double UnDistributeAmount = 0;
        if (dt.Rows.Count > 0)
        {
            this.lblCount.Visible = true;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PurchaseCount += HZ.MathUtil.SToI(dt.Rows[i]["PurchaseCount"].ToString());
                UnDistributeAmount += HZ.MathUtil.SToF(dt.Rows[i]["UnDistributeAmount"].ToString());
            }
            lblCount.Text = "统计：未配送总数量为" + PurchaseCount.ToString() + "，未配送总金额为" + UnDistributeAmount.ToString("N2") + "";
        }
        else
        {
            lblCount.Visible = false;
        }
    }



    //#region 地区选择
    //public void BindArea()
    //{
    //    IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
    //    IList<Area> lstArea = new List<Area>();
    //    string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
    //    //判断用户所属地区，如果用户是省级的 则可以列出全部区域
    //    if (userAreaId == CurrentAccount.GetCurrentDeployArea().AreaID)
    //    {
    //        lstArea = ia.GetSonAreas(userAreaId);
    //        foreach (Area area in lstArea)
    //        {
    //            if (area.AreaName == "省直属")
    //            {
    //                ListItem item = new ListItem();
    //                item.Text = area.AreaName;
    //                item.Value = area.AreaID;
    //                ddlCity.Items.Insert(0, item);
    //            }
    //            else
    //            {
    //                ListItem ddlitem = new ListItem();
    //                ddlitem.Text = area.AreaName;
    //                ddlitem.Value = area.AreaID;
    //                ddlCity.Items.Add(ddlitem);
    //            }
    //        }

    //        //ListItem a = new ListItem();
    //        //a.Text = "省直属";
    //        //a.Value = userAreaId;
    //        //ddlCity.Items.Insert(0, a);

    //        ListItem b = new ListItem();
    //        b.Text = "全部";
    //        b.Value = "";
    //        ddlCity.Items.Insert(0, b);
    //    }
    //    else
    //    {
    //        lstArea = ia.GetAreasByAreaID(userAreaId);
    //        foreach (Area area in lstArea)
    //        {
    //            ListItem ddlitem = new ListItem();
    //            ddlitem.Text = area.AreaName;
    //            ddlitem.Value = area.AreaID;
    //            ddlCity.Items.Add(ddlitem);
    //        }
    //    }
    //    //绑定数据源
    //    //如果有当前省的编号，则增加省直属 
    //    //同时地区获取值的要设置 

    //    //绑定区县
    //    ddlCity_SelectedIndexChanged(null, null);

    //}
    ///// <summary>
    ///// 地区编号
    ///// </summary>
    //public string AreaID
    //{
    //    get
    //    {
    //        if (!string.IsNullOrEmpty(ddlCountry.SelectedValue))
    //        {
    //            return ddlCountry.SelectedValue;
    //        }
    //        else
    //        {
    //            return ddlCity.SelectedValue;
    //        }
    //    }

    //    set
    //    {
    //        if (value != null)
    //        {
    //            if (value.Trim() == "")
    //            {
    //                ddlCity.SelectedIndex = 0;
    //            }
    //            else
    //            {
    //                string[] areaArray = value.ToString().Split(',');

    //                if (areaArray.Length > 1)
    //                {
    //                    ddlCity.SelectedIndex = 0;//多个地区的时候，显示全部
    //                }
    //                else
    //                {
    //                    for (int i = 0; i < ddlCity.Items.Count; i++)
    //                    {
    //                        if (ddlCity.Items[i].Value == value.Trim())
    //                        {
    //                            ddlCity.SelectedIndex = i;
    //                            break;
    //                        }
    //                    }
    //                }
    //            }
    //        }
    //    }
    //}
    //protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    string strCity = (this.ddlCity.SelectedItem != null) ? this.ddlCity.SelectedItem.Value : "";

    //    this.ddlCountry.Items.Clear();

    //    IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
    //    IList<Area> lstArea = new List<Area>();
    //    if (!string.IsNullOrEmpty(strCity))
    //    {
    //        lstArea = ia.GetSonAreas(strCity);

    //        foreach (Area area in lstArea)
    //        {
    //            ListItem ddlitem = new ListItem();
    //            ddlitem.Text = area.AreaName;
    //            ddlitem.Value = area.AreaID;
    //            this.ddlCountry.Items.Add(ddlitem);
    //        }
    //    }

    //    ListItem b = new ListItem();
    //    b.Text = "所在区县";
    //    b.Value = "";
    //    ddlCountry.Items.Insert(0, b);
    //}
    //#endregion
}
